﻿using UnityEngine;
using System.Collections.Generic;
using System;

public class WindowProxy : ViewProxy
{
    WeakReference preWindow;

    UIAnchor uiAnchor = null;
    UIAnchor.Side uiAnchorSide = UIAnchor.Side.TopLeft;
    public Vector2 uiAnchorOffset = Vector2.zero;

    protected List<ViewProxy> childList = new List<ViewProxy>();
    protected UIPanel blockClickPanel;

    protected float uiWidth = 0f;
    protected float uiHeight = 0f;

    private int _depth;
    public int depth
    {
        get
        {
            return _depth;
        }
        set
        {
            _depth = value;
        }
    }

    public override bool isActive
    {
        set
        {
            base.isActive = value;
            if (!this.isDestroyed && this._isActive && this.win == null && blockClickPanel == null)
            {
                GameObject blockClickObj = new GameObject(this.winName + "_blockClick");
                blockClickObj.layer = this.parentGo.layer;
                blockClickPanel = blockClickObj.AddComponent<UIPanel>();
                blockClickObj.transform.SetParent(this.parentGo.transform, false);
                Window.CreateBlockClick(blockClickObj.gameObject, blockClickPanel);
                blockClickPanel.depth = this.depth;
                this.win = null;
            }
        }
    }

    public override void Destroy()
    {
        if (blockClickPanel != null)
        {
            GameObject.Destroy(blockClickPanel.gameObject);
            blockClickPanel = null;
        }

        base.Destroy();
    }

    protected override void LoadComplete(object obj, string path)
    {
        if (isDestroyed)
        {
            return;
        }

        WindowProxy preWin = null;
        if(preWindow != null && preWindow.IsAlive)
        {
            preWin = preWindow.Target as WindowProxy;
        }

        if (preWin != null && this.preWindowAction != WindowManager.WinAnimActionNone)
        {
            this._parentWinManager.HideWindow(preWin, this.preWindowAction == WindowManager.WinAnimActionDestroy);
        }
            
        if (blockClickPanel != null)
        {
            GameObject.Destroy(blockClickPanel.gameObject);
            blockClickPanel = null;
        }

        InitUIGameObject(obj);

        if(isDestroyed)
        {
            return;
        }

        if(win == null)
        {
            throw new System.Exception("win is null: " + path);
        }

        // Debug.LogError("test win:" + isDestroyed + "  " + (obj == null) + "  " + (win == null) + " " + path);

        win.parentWinManager = _parentWinManager;
        win.setDepth(this.depth);

        uiAnchor = win.gameObject.GetComponent<UIAnchor>();
        if (uiAnchor != null)
        {
            uiAnchor.container = parentGo;
            uiAnchor.enabled = false;
        }

        InitView();
    }

    //@todo
    // 1. tween
    // 2. panelType

    public void SetPreWindowProxy(WindowProxy preWin)
    {
        if(preWin == null)
        {
            return;
        }

        preWindow = new WeakReference(preWin);
    }

    protected UIAnchor.Side anchorSide
    {
        set
        {
            uiAnchorSide = value;
            Relocate();
        }
        get { return uiAnchor.side; }
    }

    public virtual void Relocate()
    {
        if (uiAnchor == null || win == null)
            return;

        uiAnchor.enabled = true;
        uiAnchor.side = uiAnchorSide;
        uiWidth = uiBounds.size.x * win.gameObject.transform.localScale.x;
        uiHeight = uiBounds.size.y * win.gameObject.transform.localScale.y;
        switch (uiAnchorSide)
        {
            case UIAnchor.Side.TopLeft:
                uiAnchor.pixelOffset.x = 0;
                uiAnchor.pixelOffset.y = 0;
                break;
            case UIAnchor.Side.Top:
                uiAnchor.pixelOffset.x = -uiWidth / 2;
                uiAnchor.pixelOffset.y = 0;
                break;
            case UIAnchor.Side.TopRight:
                uiAnchor.pixelOffset.x = -uiWidth;
                uiAnchor.pixelOffset.y = 0;
                break;
            case UIAnchor.Side.Left:
                uiAnchor.pixelOffset.x = 0;
                uiAnchor.pixelOffset.y = uiHeight / 2;
                break;
            case UIAnchor.Side.Center:
                uiAnchor.pixelOffset.x = -uiWidth / 2;
                uiAnchor.pixelOffset.y = uiHeight / 2;
                break;
            case UIAnchor.Side.Right:
                uiAnchor.pixelOffset.x = -uiWidth;
                uiAnchor.pixelOffset.y = uiHeight / 2;
                break;
            case UIAnchor.Side.BottomLeft:
                uiAnchor.pixelOffset.x = 0;
                uiAnchor.pixelOffset.y = uiHeight;
                break;
            case UIAnchor.Side.Bottom:
                uiAnchor.pixelOffset.x = -uiWidth / 2;
                uiAnchor.pixelOffset.y = uiHeight;
                break;
            case UIAnchor.Side.BottomRight:
                uiAnchor.pixelOffset.x = -uiWidth;
                uiAnchor.pixelOffset.y = uiHeight;
                break;
        }
        uiAnchor.pixelOffset.x += uiAnchorOffset.x;
        uiAnchor.pixelOffset.y += uiAnchorOffset.y;
        //强制刷新一次
        uiAnchor.enabled = false;
        uiAnchor.enabled = true;
        localPos = win.gameObject.transform.localPosition;
    }

    public int GetChildIndex(ViewProxy com)
    {
        return childList.IndexOf(com);
    }

    public T AddChild<T>(params object[] paramlist) where T : ViewProxy
    {
        ViewProxy com = new ViewProxy();
        childList.Add(com);
        com.parentGo = win.gameObject;
        com.isActive = true;
        return com as T;
    }

    public T AddChildAt<T>(int index, params object[] paramlist) where T : ViewProxy
    {
        ViewProxy com = new ViewProxy();
        childList.Insert(index, com);
        com.parentGo = win.gameObject;
        com.isActive = true;
        return com as T;
    }

    public ViewProxy AddChildByType(Type type, params object[] paramlist)
    {
        ViewProxy com = new ViewProxy();
        childList.Add(com);
        com.parentGo = win.gameObject;
        com.isActive = true;
        return com;
    }

    public ViewProxy AddChildAtByType(Type type, int index, params object[] paramlist)
    {
        ViewProxy com = new ViewProxy();
        childList.Insert(index, com);
        com.parentGo = win.gameObject;
        com.isActive = true;
        return com;
    }

    public void RemoveChild(ViewProxy com)
    {
        if (!childList.Remove(com))
            return;
        com.isActive = false;
        com.Destroy();
    }

    public void OnTop(bool isTop)
    {
        
    }

    public override void Close()
    {
        this.Close(null, false);
    }

    public override void Close(IUIAnimElem animElem, bool forward)
    {
        if (this.win != null)
        {
            this.win.Close(animElem, forward);
        }
        else
        {
            _parentWinManager.HideWindow(this);
        }
    }
}

